<?php
declare(strict_types=1);

namespace App\Admin\Controller\System\Permission;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Request\System\Menu\SystemMenuCreateRequest;
use App\Admin\Service\System\SystemMenuService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieController;
use support\Log;
use support\Request;
use support\Response;


/**
 * Class MenuController
 * @package App\system\Controller
 */
#[Controller(prefix: "system/menu"), Auth]
class MenuController extends NewbieController {
    #[Inject]
    protected SystemMenuService $service;

    /**
     * 菜单树列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("system:menu:index")]
    public function index (Request $request): Response {
        return $this->success($this->service->getTreeList($request->all()));
    }

    /**
     * 回收站菜单树列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("recycle"), Permission("system:menu:recycle")]
    public function recycle (Request $request): Response {
        return $this->success($this->service->getTreeListByRecycle($request->all()));
    }

    /**
     * 前端选择树（不需要权限）
     * @param Request $request
     * @return Response
     */
    #[GetMapping("tree")]
    public function tree (Request $request): Response {
        return $this->success($this->service->getSelectTree($request->all()));
    }

    /**
     * 新增菜单
     * @param SystemMenuCreateRequest $request
     * @return Response
     */
    #[PostMapping("save"), Permission("system:menu:save")]
    public function save (SystemMenuCreateRequest $request): Response {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 更新菜单
     * @param int $id
     * @param SystemMenuCreateRequest $request
     * @return Response
     */
    #[PutMapping("update/{id}"), Permission("system:menu:update")]
    public function update (int $id, SystemMenuCreateRequest $request): Response {
        return $this->service->update($id, $request->all())
            ? $this->success() : $this->error(t('newbieadmin.data_no_change'));
    }

    /**
     * 单个或批量删除菜单到回收站
     * @param Request $request
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("delete/{ids}"), Permission("system:menu:delete")]
    public function delete (Request $request, string $ids): Response {
        Log::info($ids);
        return $this->service->delete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量真实删除菜单 （清空回收站）
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("realDelete/{ids}"), Permission("system:menu:realDelete")]
    public function realDelete (string $ids): Response {
        $menus = $this->service->realDel($ids);
        return is_null($menus) ?
            $this->success() :
            $this->success(t('system.exists_children_ctu', ['names' => implode(',', $menus)]));
    }

    /**
     * 单个或批量恢复在回收站的菜单
     * @param String $ids
     * @return Response
     */
    #[PutMapping("recovery/{ids}"), Permission("system:menu:recovery")]
    public function recovery (string $ids): Response {
        return $this->service->recovery($ids) ? $this->success() : $this->error();
    }
}